package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.common.enums.TaskTypeEnum;
import com.kingyun.gpsinspection.purificationservice.common.enums.WorkOrderStatusEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.ManageWorkOrderMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ManageWorkOrder;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ManageWorkOrderVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.WorkOrderStatistics;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.ManageWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务工单管理service
 * Created by jiangjx on 2017/2/10.
 */
public class ManageWorkOrderServiceImpl implements ManageWorkOrderService {

    @Autowired
    private ManageWorkOrderMapper manageWorkOrderMapper;

    /**
     * 加载工单管理表格信息
     *
     * @param employeeVo
     * @param manageWorkOrderVo 查询条件封装类
     * @return
     */
    @Override
    public JSONObject loadManageWorkOrderTable(EmployeeVo employeeVo, ManageWorkOrderVo manageWorkOrderVo) {
         /*装载查询条件*/
        manageWorkOrderVo.setLimit(null == manageWorkOrderVo.getLimit() ? 10 : manageWorkOrderVo.getLimit());
        manageWorkOrderVo.setOffset(null == manageWorkOrderVo.getOffset() ? 0 : manageWorkOrderVo.getOffset());
        JSONObject jsonObject = new JSONObject();
        //设置过滤条件
        manageWorkOrderVo.setFilterEmployeeId(employeeVo.getEmployeeId());
        manageWorkOrderVo.setFilterOrgId(employeeVo.getOrgId());
        /* 进行分页查询分离 */
        jsonObject.put("total", manageWorkOrderMapper.queryCount(manageWorkOrderVo));//查询总条数
        List<ManageWorkOrder> baseRows = manageWorkOrderMapper.queryTableList(manageWorkOrderVo);//查询分页内容
        /*返回结果*/
        jsonObject.put("rows", JSON.toJSON(baseRows));
        return jsonObject;
    }

    /**
     * 获取工单所属任务类型的柱状图
     *
     * @param employeeVo
     * @return
     */
    @Override
    public String queryWorkOrderTypePieChartData(EmployeeVo employeeVo) {
        JSONObject jsonObject = new JSONObject();
        Map<String, BigDecimal> map = new HashMap<>();
        ManageWorkOrderVo manageWorkOrderVo = new ManageWorkOrderVo();
        //设置过滤条件
//        manageWorkOrderVo.setFilterEmployeeId(employeeVo.getEmployeeId());
//        manageWorkOrderVo.setFilterOrgId(employeeVo.getOrgId());
        WorkOrderStatistics workOrderStatistics = manageWorkOrderMapper.selectWorkOrderStatistics(manageWorkOrderVo);
        for (TaskTypeEnum typeEnum : TaskTypeEnum.values()) {
            switch (typeEnum.getCode()) {
                case 1:
                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask1());
                    break;
                case 2:
                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask2());
                    break;
                case 3:
                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask3());
                    break;
//                case 4:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask4());
//                    break;
                case 5:
                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask5());
                    break;
//                case 6:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask6());
//                    break;
//                case 7:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask7());
//                    break;
//                case 8:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask8());
//                    break;
//                case 9:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask9());
//                    break;
//                case 10:
//                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask10());
//                    break;
                case 11:
                    map.put(typeEnum.getDecription(), workOrderStatistics.getTask11());
                    break;
            }
        }
        JSONArray x = new JSONArray();
        JSONArray y = new JSONArray();
        for (String key : map.keySet()) {
            x.add(key);
            y.add(map.get(key));
        }
        //封装Y轴数据
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("color", "#F1565E");
        object.put("name", "工单");
        object.put("data", y);
        array.add(object);
        //封装最后一层
        jsonObject.put("x", x);
        jsonObject.put("y", array);
        return jsonObject.toString();
    }

    /**
     * 获取工单状态的饼图data
     *
     * @param employeeVo
     * @return
     */
    @Override
    public String queryWorkOrderStatusPieChartData(EmployeeVo employeeVo) {
        JSONObject jsonObject = new JSONObject();
        Map<String, BigDecimal> map = new HashMap<>();
        ManageWorkOrderVo manageWorkOrderVo = new ManageWorkOrderVo();
        //设置过滤条件
//        manageWorkOrderVo.setFilterEmployeeId(employeeVo.getEmployeeId());
//        manageWorkOrderVo.setFilterOrgId(employeeVo.getOrgId());
        WorkOrderStatistics workOrderStatistics = manageWorkOrderMapper.selectWorkOrderStatistics(manageWorkOrderVo);
        for (WorkOrderStatusEnum statusEnum : WorkOrderStatusEnum.values()) {
            switch (statusEnum.getCode()) {
                case 0:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus0());
                    break;
                case 1:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus1());
                    break;
                case 2:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus2());
                    break;
                case 3:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus3());
                    break;
                case 4:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus4());
                    break;
                case 5:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus5());
                    break;
                case 6:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus6());
                    break;
                case 7:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus7());
                    break;
                case -1:
                    map.put(statusEnum.getDecription(), workOrderStatistics.getStatus8());
                    break;
            }
        }
        JSONArray x = new JSONArray();
        JSONArray y = new JSONArray();
        for (String key : map.keySet()) {
            x.add(key);
            y.add(map.get(key));
        }
        //封装Y轴数据
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("color", "#F1565E");
        object.put("name", "工单");
        object.put("data", y);
        array.add(object);
        //封装最后一层
        jsonObject.put("x", x);
        jsonObject.put("y", array);
        return jsonObject.toString();
    }


}
